<?php


namespace app\btApi\controller;


use app\btApi\validate\UseraddValidate;
use app\common\service\UserService;
use app\common\utils\ResponseUtils;
use app\common\utils\SocketUtils;
use think\facade\Request;
use hg\apidoc\annotation as Apidoc;
use think\log\driver\Socket;

/**
 * @Apidoc\Title("用户模块")
 */
class User
{
    private $userService;
    private $socketUtils;
    public function __construct()
    {
        $this->socketUtils=new SocketUtils();
        $this->userService=new UserService();
    }
    public function bind(){
        $data=Request::post();
        $uid=(request()->userInfo)['uid'];
        $this->socketUtils->bindClientIdToMemberId($uid,$data['clientId']);
    }
    /**
     * @Apidoc\Title("创建用户")
     * @Apidoc\Desc("这里是一个描述")
     * @Apidoc\Url("btApi/user/add")
     * @Apidoc\Method("POST")
     * @Apidoc\Tag("删除用户")
     * @Apidoc\Param("uid", type="int",require=true, desc="用户ID" )
     */
    public function del(){
        $this->userService->del(Request::post('uid'));
        return ResponseUtils::retSuccess();
    }
    /**
     * @Apidoc\Title("创建用户")
     * @Apidoc\Desc("这里是一个描述")
     * @Apidoc\Url("btApi/user/add")
     * @Apidoc\Method("POST")
     * @Apidoc\Tag("创建用户")
     * @Apidoc\Param("username", type="string",require=true, desc="用户名" )
     * @Apidoc\Param("password",type="string",require=true,desc="用户密码")
     * @Apidoc\Param("avatar",type="string",require=true,desc="头像地址")
     * @Apidoc\Param("rolejson",type="string",require=true,desc="角色json")
     * @Apidoc\Param("depjson",type="string",require=true,desc="部门json")
     * @Apidoc\Param("groupjson",type="string",require=true,desc="权限组json")
     * @Apidoc\Returned("data", type="array", desc="数据",
     *    @Apidoc\Returned("token", type="string", desc="登录身份令牌")
     * )
     */
    public function add(){
        $data=Request::post();
        $data['create_time']=date('Y-m-d H:i:s');
        try {
            validate(UseraddValidate::class)->check(Request::param());
            $this->userService->create($data);
            return ResponseUtils::retSuccess();
        }catch (\Exception $exception){
            return ResponseUtils::retError($exception->getMessage());
        }
    }
    /**
     * @Apidoc\Title("用户列表")
     * @Apidoc\Desc("这里是一个用户列表的接口")
     * @Apidoc\Url("btApi/user/list")
     * @Apidoc\Method("POST")
     * @Apidoc\Tag("创建用户")
     * @Apidoc\Param("username", type="string",require=true, desc="用户名" )
     * @Apidoc\Param("avatar",type="string",require=true,desc="头像地址")
     * @Apidoc\Returned("data", type="array", desc="数据",
     *    @Apidoc\Returned("id", type="int", desc="ID"),
     *    @Apidoc\Returned("avatar", type="string", desc="头像"),
     *    @Apidoc\Returned("username", type="string", desc="用户名"),
     * )
     */
    public function list(){
        $query=[];
        $query[]=['username','=',Request::post('username')];
        $data=$this->userService->list(Request::post('page'),Request::post('limit'),$query);
        return ResponseUtils::retSuccessWithListData($data);
    }

}